package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ldatum.crm.dao.TContactsDao;
import com.ldatum.crm.domain.TContacts;
import com.ldatum.crm.util.HibernateUtil;

public class TContactsDaoImpl implements TContactsDao {

	@Override
	public void add(TContacts contacts) throws Exception {
		HibernateUtil.add(contacts);
	}

	@Override
	public void delete(TContacts contacts, int id) throws Exception {
		HibernateUtil.delete(contacts);
	}

	@Override
	public void update(TContacts contacts, int id) throws Exception {
		HibernateUtil.update(contacts);
	}

	@Override
	public TContacts findById(Class<TContacts> clazz, int id) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			TContacts contacts = (TContacts) s.get(clazz, id);
			contacts.setCustomer(null);
			return contacts;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
	@Override
	public TContacts queryById(Class<TContacts> clazz, int id) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			TContacts contacts = (TContacts) s.get(clazz, id);
			Hibernate.initialize(contacts.getCustomer());
			return contacts;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

	@Override
	public List<TContacts> list(Class<TContacts> clazz,String employee ) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TContacts as contacts";
			Query q = s.createQuery(hql);
			@SuppressWarnings("unchecked")
			List<TContacts> list = (List<TContacts>) q.list();
			//懒加载处理
			for(TContacts con:list){
				Hibernate.initialize(con.getCustomer());
			}
			return list;
		}finally{
			if(s!=null){
				s.close();
			}

		}
	}

	@Override
	public TContacts findNameByNo(String id) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TContacts as contacts where customer_id =:id and type = :type ";
			Query q = s.createQuery(hql);
			q.setString("id", id);
			q.setString("type", "Importance");
			@SuppressWarnings("unchecked")
			List<TContacts> contacts = (List<TContacts>) q.list();
			if(contacts.size()==0){
				return null;
			}else{
				return contacts.get(0);
			}
			
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public List<TContacts> findByNo(Class<TContacts> clazz, String no) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TContacts as contacts where cusNo =:no";
			Query q = s.createQuery(hql);
			q.setString("no", no);
			@SuppressWarnings("unchecked")
			List<TContacts> list = (List<TContacts>) q.list();
			return list;
		}finally{
			if(s!=null)
			s.close();
		}
	}
	
	@Override
	public void deletes(String ids) throws Exception {
		Session s = null;
		Transaction tx = null;
		ids = ids.substring(0,ids.length()-1);
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String hql = "delete from TContacts as contacts where id in ("+ids+")";
			Query q = s.createQuery(hql);
			q.executeUpdate();
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public List<TContacts> findByCustomerId(int customerId) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TContacts as contacts where customer_id =:id";
			Query q = s.createQuery(hql);
			q.setInteger("id", customerId);
			@SuppressWarnings("unchecked")
			List<TContacts> list = (List<TContacts>) q.list();
			return list;
		}finally{
			if(s!=null)
			s.close();
		}
	}
	
}
